package com.tsky.yyj.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.tsky.casing.action.FrontAction;
import com.tsky.common.ActionUtil;
import com.tsky.common.BeanUtil;
import com.tsky.common.MD5Util;
import com.tsky.common.RandomValidateCode;
import com.tsky.yyj.entity.Consumer;
import com.tsky.yyj.manager.ConsumerManager;

@Component("consumerAction")
public class ConsumerAction extends FrontAction {
	@Autowired
	private ConsumerManager consumerManager;
	

	/* bean properties */
	private String id;// 序号
	private String code;// 用户名
	private String password;// 密码
	private String name;// 姓名
	private String nickname;
	
	private String sex;// 性别
	private String birthday;// 生日
	private String binding;  //是否绑定手机
	private String phone;// 手机号
	private String telephone;// 座机
	private String qq;// qq
	
	
	public void grid() {
		
	}
    /**
     * 会员注册
     */
	public void register() {
		
		//验证码验证
		String randomCode = ActionUtil.getParameter("randomCode");
		RandomValidateCode randomValidateCode = new RandomValidateCode();
		if(!randomValidateCode.valid(ActionUtil.getRequest(), randomCode)){
			//验证码错误
			ActionUtil.ajaxWrite("-1");
			return;
		}
		//判断是否唯一
		Object obj = consumerManager.queryByCode(code);
		if(obj != null){
			//验证码错误
			ActionUtil.ajaxWrite("-2");
			return;
		}
		//保存
		Consumer entity = new Consumer();
		BeanUtil.copyProperties(entity, this);
		entity.setId(BeanUtil.getID());
		entity.setPassword(MD5Util.getMD5(password));
		boolean result = consumerManager.saveEntity(entity);
		ActionUtil.ajaxWrite("1");
	}
	 /**
     * 会员登录
     */
	public void login() {
		
		//验证码验证
		String randomCode = ActionUtil.getParameter("randomCode");
		RandomValidateCode randomValidateCode = new RandomValidateCode();
		if(!randomValidateCode.valid(ActionUtil.getRequest(), randomCode)){
			//验证码错误
			ActionUtil.ajaxWrite("-1");
			return;
		}
		Consumer consumer = (Consumer)consumerManager.userLogin(code, MD5Util.getMD5(password));
		if(consumer == null){
			//用户名或者密码错误
			ActionUtil.ajaxWrite("-2");
			return;
		}
		ActionUtil.setToSession("loginConsumer", consumer);
		
		//用户名或者密码错误
		ActionUtil.ajaxWrite("1");

	}

    /**
     * 会员查询
     * @return
     */
	public String query() {
		Consumer entity = consumerManager.getEntity(Consumer.class, id);
		ActionUtil.setToEL("agentEnt", entity);
		return "query";
	}
	 /**
     * 会员更新
     * @return
     */
	public void update() {
		Consumer entity = consumerManager.getEntity(Consumer.class, id);
		BeanUtil.copyProperties(entity, this);

		boolean result = consumerManager.updateEntity(entity);
		ActionUtil.ajaxWrite(result);
	}

	
	public ConsumerManager getConsumerManager() {
		return consumerManager;
	}
	public void setConsumerManager(ConsumerManager consumerManager) {
		this.consumerManager = consumerManager;
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public String getBirthday() {
		return birthday;
	}
	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}
	public String getBinding() {
		return binding;
	}
	public void setBinding(String binding) {
		this.binding = binding;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getQq() {
		return qq;
	}
	public void setQq(String qq) {
		this.qq = qq;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public static void main(String[] args) {
		Consumer consumer = (Consumer)null;
		System.out.println(consumer);
	}
}
